package com.lcf.user.delay;

import java.time.LocalTime;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DelayConsumer {
    // 延时队列
    private DelayQueue<Message> queue;

    public DelayConsumer(DelayQueue<Message> queue) {
        this.queue = queue;
    }

    ExecutorService threadPool = Executors.newSingleThreadExecutor();

    public void run() {
        threadPool.execute(() -> {
            while (true) {
                try {
                    Message take = queue.take();
                    if (take.getNum() > 3) {
                        break;
                    }
                    Message mm = new Message(1, "world", 3000, take.getNum() + 1);
                    queue.offer(mm);
                    System.out.println("当前threadPool线程：" + Thread.currentThread().getName());
                    System.out.println("执行时间为 : " + LocalTime.now() + ", body : " + take.getBody() + ", num : " + take.getNum());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
